﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;
namespace App.Controllers
{
    public class CremationServiceController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;

            int offset = (page ?? 1 - 1) * (rows ?? 10);
            PagedList<tb_CremationRecords> list = null;
            if ("asc" == order)
            {
                list = dbe.tb_CremationRecords.OrderBy(m => m.CremationNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            else
            {
                list = dbe.tb_CremationRecords.OrderByDescending(m => m.CremationNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            PagedList<CremationServiceEditViewModel> data = new PagedList<CremationServiceEditViewModel>(
                                                list.CurrentPageIndex, list.PageSize, list.total);
            data.rows.AddRange(from f in list.rows
                               select new CremationServiceEditViewModel
                               {
                                   CremationNO = f.CremationNO,                                   
                                   ServiceNo = f.ServiceNo,
                                   DeadNO = dbe.tb_ServiceDetail.FirstOrDefault(m => m.ServiceNo == f.ServiceNo).DeadNO,
                                   PermitDate = f.PermitDate,
                                   AshesHandle = f.AshesHandle
                               });

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var vmodel = GetServiceItemList("", Utils.CREMATION_SERVICE_TYPE);
            return PartialView("UCServicesItemList", vmodel);
        }
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "ItemList")]CremationServiceEditViewModel vmodel)
        {
            try
            {
                var serviceNo = GenServiceNo();
                //保存服务费用
                AddServiceItems(vmodel.DeadNO, serviceNo, vmodel.SelectedItemList);

                //保存告别厅使用记录
                var m = new tb_CremationRecords
                {
                    CremationNO = GenCremationNO(),
                    ServiceNo = serviceNo,
                    PermitDate = vmodel.PermitDate,
                    AshesHandle = vmodel.AshesHandle
                };
                dbe.tb_CremationRecords.AddObject(m);
                dbe.SaveChanges();
                CurrentDeader = vmodel.DeadNO;
                return Json(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { success = false, msg = "保存出错！" }, JsonRequestBehavior.AllowGet);
            }
        }
        
       
        public ActionResult Edit(string id)
        {
            var model = dbe.tb_CremationRecords.FirstOrDefault(m => m.CremationNO == id);
            var vmodel = GetServiceItemList(model.ServiceNo, Utils.CREMATION_SERVICE_TYPE);
            return PartialView("UCServicesItemList", vmodel);
        }

        [HttpPost]
        public ActionResult Edit(string id, [Bind(Exclude = "ItemList")]CremationServiceEditViewModel vmodel)
        {
            try
            {
                EditServiceItems(vmodel.SelectedItemList, vmodel.DeadNO, vmodel.ServiceNo);

                var model = dbe.tb_CremationRecords.FirstOrDefault(m => m.CremationNO == vmodel.CremationNO);
                model.PermitDate = vmodel.PermitDate;
                model.AshesHandle = vmodel.AshesHandle;
                dbe.SaveChanges();
                CurrentDeader = vmodel.DeadNO;
                return Json(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { success = false, msg = "保存出错！" }, JsonRequestBehavior.AllowGet);
            }            
        }
        
        [HttpPost]
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                foreach (var id in idList)
                {
                    var obj = dbe.tb_CremationRecords.FirstOrDefault(m => m.CremationNO == id);
                    dbe.tb_CremationRecords.DeleteObject(obj);
                    //删除服务收费
                    DeleteServiceItems(obj.ServiceNo);
                }

                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "删除出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }          
    }
}
